#!/usr/bin/perl
# (c) 2013 - Xavier Berger - http://rpi-experiences.blogspot.fr/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
@ARGV or die "USAGE: $0 [conf] [version] | gzip -c > program.1.gz";

my $conf=shift or die "You must specify the configuration file to open\n";
-f $conf or die "$conf is not existing\n";
my $version=shift or die "You must specify the version\n";

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $datestring = sprintf("%4d-%02d-%02d",($year + 1900),($mon+1),$mday);

print ".\" Manpage for generated by conf2man.pl.\n";
$conf =~ /([^\/]+)$/;
print ".TH man 5 \"$datestring\" \"$version\" \"$1 man page\"\n";
open(FILE, "$conf") 
  or die "Can't open $conf\n";
while (<FILE>) {
  /#$/ or /# / or /^$/ or next;
  s/(# |^#*#$)//g;
  /^\S\w/ and print ".SH ";
  print "$_";
} 
close(FILE);
